//
//  XGNavigationController.m
//  BSBuDeJie
//
//  Created by monkey on 2018/12/10.
//  Copyright © 2018 itcast. All rights reserved.
//

#import "XGNavigationController.h"

@interface XGNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation XGNavigationController

#pragma mark - 控制器生命周期方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 添加滑动退出控制器手势
    [self addGestureRecognizer];
}

- (void)addGestureRecognizer
{
    id target = [[self.interactivePopGestureRecognizer valueForKey:@"targets"] lastObject];
    
    // 禁用系统手势
    self.interactivePopGestureRecognizer.enabled = NO;
    
    // 添加手势 实现全屏滑动返回功能
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wundeclared-selector"
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:[target valueForKey:@"target"] action:@selector(handleNavigationTransition:)];
    #pragma clang diagnostic pop
    
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
}

#pragma mark - UIGestureRecognizerDelegate

// 手势是否可用 如果是根控制器禁用滑动返回 如果是非根控制器 手势可用
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return self.childViewControllers.count > 1;
}

+ (void)initialize
{
 
   UINavigationBar *navigationBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[self]];
    
    [navigationBar setShadowImage:[UIImage new]];
   // 设置导航栏标题样式
    NSDictionary *attributes = @{
                                 NSForegroundColorAttributeName : [UIColor blackColor],
                                 NSFontAttributeName : [UIFont systemFontOfSize:25]};
    [navigationBar setTitleTextAttributes:attributes];
    
    // 设置导航栏渲染颜色
    navigationBar.tintColor = [UIColor darkGrayColor];
}

#pragma mark - 重写方法

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    [super pushViewController:viewController animated:animated];
}

@end
